package cn.edu.imau.zy.student.controller;

import cn.edu.imau.zy.common.constant.ActivitiConstant;
import cn.edu.imau.zy.common.constant.ConfigConstant;
import cn.edu.imau.zy.common.constant.DictConstant;
import cn.edu.imau.zy.common.core.entity.activiti.BizBusiness;
import cn.edu.imau.zy.common.utils.StringUtils;
import cn.edu.imau.zy.student.service.IStStudentPoorService;
import cn.edu.imau.zy.system.feign.RemoteActivitiService;
import cn.edu.imau.zy.system.feign.RemoteConfigService;
import cn.hutool.core.util.IdUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.common.core.entity.student.StStudentPoor;

import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.controller.BaseController;

import java.util.Date;
import java.util.Map;

/**
 * 贫困信息 提供者
 *
 * @author Wyz
 * @date 2020-10-16
 */
@RestController
@RequestMapping("poor")
public class StStudentPoorController extends BaseController {

    @Autowired
    private IStStudentPoorService stStudentPoorService;
    @Autowired
    private RemoteConfigService configService;
    @Autowired
    private RemoteActivitiService activitiService;

    /**
     * 查询${tableComment}
     */
    @GetMapping("get/{id}")
    public StStudentPoor get(@PathVariable("id") String id) {
        return stStudentPoorService.selectStStudentPoorById(id);
    }

    /**
     * 分析新建还是修改
     */
    @GetMapping("analysis")
    public R analysis(@LoginUser SysUser user) {
        StStudentPoor studentPoor = new StStudentPoor();
        studentPoor.setYear(configService.selectConfigByKey(ConfigConstant.STUDENT_POOR_YEAR));
        studentPoor.setStudentId(user.getUserId());
        if (user.getRoleIds().contains(5L)) {
            studentPoor = stStudentPoorService.selectStStudentPoor(studentPoor);
            if (studentPoor != null && StringUtils.isNotEmpty(studentPoor.getId())) {
                return R.ok().put("id", studentPoor.getId());
            }
            return R.ok().put("id", 0);
        }
        return R.error("对不起，您无权操作！");
    }

    /**
     * 提交审批
     */
    @GetMapping("/start/{id}")
    @GlobalTransactional
    public R start(@PathVariable("id") String id, @LoginUser SysUser user) {
        StStudentPoor studentPoor = new StStudentPoor();
        studentPoor.setId(id);
        studentPoor.setStatus(DictConstant.POOR_STATUS.APPROVAL);
        stStudentPoorService.updateStStudentPoor(studentPoor);

        Map<String, String> ref = activitiService.oneLatest("studentPoor");
        BizBusiness business = initBusiness(id, user, ref);
        R r = activitiService.startProcess(business);
        if (!r.equals(R.ok())) {
            return R.error();
        }

        return r;
    }

    @GetMapping("biz/{businessKey}")
    public R biz(@PathVariable("businessKey") String businessKey) {
        BizBusiness business = activitiService.getBizBusinessById(businessKey);
        if (null != business) {
            StStudentPoor studentPoor = stStudentPoorService.selectStStudentPoorById(business.getTableId());
            return R.data(studentPoor);
        }
        return R.error("no record");
    }

    /**
     * @param tableId 业务ID
     * @param user    申请用户
     * @param ref     流程信息
     * @return
     * @author zmr
     */
    private BizBusiness initBusiness(String tableId, SysUser user, Map<String, String> ref) {
        BizBusiness business = new BizBusiness();
        business.setTableId(tableId);
        business.setProcDefId(ref.get("id"));
        business.setTitle(user.getUserName() + "的贫困申请");
        business.setProcName(ref.get("name"));
        business.setUserId(user.getUserId());
        business.setApplyer(user.getUserName());
        business.setStatus(ActivitiConstant.STATUS_DEALING);
        business.setResult(ActivitiConstant.RESULT_DEALING);
        business.setApplyTime(new Date());
        return business;
    }

    /**
     * 查询贫困信息列表
     */
    @GetMapping("list")
    public R list(StStudentPoor stStudentPoor) {
        startPage();
        return result(stStudentPoorService.selectStStudentPoorList(stStudentPoor));
    }


    /**
     * 新增保存贫困信息
     */
    @PostMapping("save")
    public R addSave(@RequestBody StStudentPoor stStudentPoor, @LoginUser SysUser user) {
        stStudentPoor.setId(IdUtil.getSnowflake(2L, 2L).nextIdStr());
        stStudentPoor.setCreateBy(user.getUserId());
        stStudentPoor.setStatus(DictConstant.POOR_STATUS.DEFAULT);
        return toAjax(stStudentPoorService.insertStStudentPoor(stStudentPoor));
    }

    /**
     * 修改保存贫困信息
     */
    @PostMapping("update")
    public R editSave(@RequestBody StStudentPoor stStudentPoor, @LoginUser SysUser user) {
        stStudentPoor.setUpdateBy(user.getUserId());
        return toAjax(stStudentPoorService.updateStStudentPoor(stStudentPoor));
    }

    /**
     * 删除${tableComment}
     */
    @PostMapping("remove")
    public R remove(String ids) {
        return toAjax(stStudentPoorService.deleteStStudentPoorByIds(ids));
    }

}
